package appli.launcher;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import system.DebugOptionInterface;
import system.util.Loader;
import appli.agentImpl.Buyer;
import appli.product.ProductOrder;

/**
 * Start a new Buyer agent
 */
public class LauncherBuyer implements DebugOptionInterface {

	/**
	 * @param args {agentName, host, port}
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		try {
			Loader l = new Loader(Buyer.class.getName());
			Class<Buyer> c = l.loadClass(Buyer.class.getName(), true);
			//we take the constructor with following parameters : 
			//String agentName, String host, int port, List<ProductOrder> productToBuy
			Constructor<Buyer> constructor = c.getConstructor(new Class[]{String.class, String.class, int.class, List.class});
			String agentName = args[0];
			String host = args[1];
			int port = Integer.parseInt(args[2]);
			List<ProductOrder> products = LauncherBuyer.getListProductOrders(agentName);
			Buyer s = constructor.newInstance(new Object[]{agentName, host, port, products});
			if (OPTION) { 
				System.out.println("Acheteur démarré sur : " + s.getCurrentHost() + ":" + s.getCurrentPort());
				System.out.println("Liste des produits à acheter : ");
				for(ProductOrder po : products)
					System.out.println("\tnom = " + po.productName + ", quantité = " + po.quantity);
			}
			s.main();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * this method is used to test different product order list for different buyers
	 * @param buyerName name of the buyer
	 * @return list of products to buy
	 */
	public static List<ProductOrder> getListProductOrders(String buyerName) {
		List<ProductOrder> productOrders = new ArrayList<ProductOrder>();
		if (buyerName.equals("Riche")) {
			productOrders.add(new ProductOrder("caviar", 2));
			productOrders.add(new ProductOrder("beaujolais nouveau", 4));
			productOrders.add(new ProductOrder("tartiflette", 6));
		}
		else if (buyerName.equals("Normal")) {
			productOrders.add(new ProductOrder("foie gras", 2));
			productOrders.add(new ProductOrder("raclette", 4));
			productOrders.add(new ProductOrder("cornichon", 6));
		}
		else if (buyerName.equals("Pauvre")) {
			productOrders.add(new ProductOrder("fondue", 2));
			productOrders.add(new ProductOrder("olives", 4));
			productOrders.add(new ProductOrder("soupe aux choux", 6));
		}
		return productOrders;
	}
}
